package com.servlet.employee;

import com.bean.Employee;
import com.service.AdminService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 
 * @author huawei
 *
 */
@WebServlet("/admin/addEmployee")
public class AddEmployeeServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        AdminService adminService = new AdminService();
        String id = req.getParameter("id");
        String name=req.getParameter("name");
        String deptid=req.getParameter("deptid");

        if (id.length() != 5||!adminService.judgeContainsStr(id)){
            req.setAttribute("Message", "员工号不满足要求，添加失败！");
            req.getRequestDispatcher("adminadduser.jsp").forward(req, resp);
            return;
        }
        if (deptid.length() != 2||!adminService.judgeContainsStr(deptid)){
            req.setAttribute("Message", "部门号不满足要求，添加失败！");
            req.getRequestDispatcher("adminadduser.jsp").forward(req, resp);
            return;
        }
        if (!adminService.getDept(deptid)){
            req.setAttribute("Message", "部门不存在，添加失败！");
            req.getRequestDispatcher("adminadduser.jsp").forward(req, resp);
            return;
        }
        boolean ismanager=req.getParameter("ismanager").equals("true")?true:false;
        Employee employee = new Employee(id,name,"1234",deptid,true,ismanager);

        if (adminService.addEmployee(employee)){
            req.setAttribute("Message","添加成功！");
            req.setAttribute("employee",employee);
            req.getRequestDispatcher("admingetuser.jsp").forward(req, resp);
        }else {
            req.setAttribute("Message","职工号重复，添加失败！");
            req.getRequestDispatcher("adminadduser.jsp").forward(req, resp);
        }
    }
}
